package com.one.common.view.pagestate.listpage;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.one.common.R;
import com.one.common.utils.Logger;
import com.one.common.view.pagestate.refreshlayout.IHeaderView;
import com.one.common.view.pagestate.refreshlayout.OnAnimEndListener;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2018/11/21 15:11
 * Description：下拉更多 首页专用
 **/
public class HeaderView2 extends LinearLayout implements IHeaderView {
    private LottieAnimationView animationView1;
    private LottieAnimationView animationView2;

    public HeaderView2(Context context) {
        super(context);


    }

    @Override
    public View getView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_header_view_2, this);
        animationView1 = findViewById(R.id.lottie_header_1);
        animationView2 = findViewById(R.id.lottie_header_2);
        return view;
    }


    @Override
    public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) {
        if (!animationView1.isShown()) {
            animationView1.setVisibility(VISIBLE);
        }
        if (animationView2.isShown()) {
            animationView2.setVisibility(GONE);
        }
        if (fraction >= 1.0f) {
            animationView1.setProgress(1.0f);
        } else {
            animationView1.setProgress(fraction);
        }

    }

    @Override
    public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
        if (!animationView1.isShown()) {
            animationView1.setVisibility(VISIBLE);
        }
        if (animationView2.isShown()) {
            animationView2.setVisibility(GONE);
        }
        if (fraction >= 1.0f) {
            animationView1.setProgress(1.0f);
        } else {
            animationView1.setProgress(fraction);
        }
    }

    @Override
    public void startAnim(float maxHeadHeight, float headHeight) {
        Logger.d("开始动画了");
        if (!animationView2.isShown()) {
            animationView2.setVisibility(VISIBLE);
        }
        if (animationView1.isShown()) {
            animationView1.setVisibility(GONE);
        }
        animationView2.playAnimation();
    }

    @Override
    public void onFinish(OnAnimEndListener animEndListener) {
        animationView2.animate().translationX(0).translationY(0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                reset();
                animEndListener.onAnimEnd();
            }
        }).start();
    }

    @Override
    public void reset() {
        animationView2.setProgress(0);
        animationView2.cancelAnimation();
        animationView1.setVisibility(VISIBLE);
        animationView2.setVisibility(GONE);
    }
}
